#include<iostream>
#include<string>
#include<cstdlib>
#include<unistd.h>
#include<pthread.h>
using namespace std;
/*
std::string toHex(pthread_t tid){
    char hex[64];
    snprintf(hex,sizeof(hex),"%p",tid);
    return hex;
}

void *threadRoutine(void *args){
    while(true){
        cout<<"thread id: "<<toHex(pthread_self())<<endl;
        sleep(1);
    }
}

int main(){
    pthread_t tid;
    pthread_create(&tid,nullptr,threadRoutine,(void*)"thread 1");
    cout<<"main thread create thread done, new thread id: "<< toHex(tid)<<endl;
    pthread_join(tid,nullptr);

    return 0;
}
*/

#include<thread>
//part2 ：c++语言的库
// void  threadrun(){
//     while(true){
//         std::cout<<"I am a new thead for C++"<<std::endl;
//         sleep(1);
//     }
// }

// int main(){
//     thread t1(threadrun);
    
//     t1.join();

//     return 0;
// }

//part3: 
// class Request{
// public:
//     Request(int start, int end, const string & threadname)
//     :start_(start),end_(end),threadname_(threadname)
//     {}

// public:
//     int start_;
//     int end_;
//     string threadname_;
// };

// class Response{
// public:
//     Response(int result,int exitcode):result_(result),exitcode_(exitcode)
//     {}
// public:
//     int result_;
//     int exitcode_;
// };

// void *sumCount(void *args){
//     Request *rq = static_cast<Request*>(args);
//     Response *rsp = new Response(0,0);
//     for(int i=rq->start_; i<=rq->end_;i++){
//         cout<< rq->threadname_<<"is runing, calling..., "<<i<<endl;
//         rsp->result_ +=i;
//         usleep(100000);
//     }
//     delete rq;
//     return rsp;
// }

// int main(){
//     pthread_t tid;
//     Request *rq = new Request(1,100,"thread 1");
//     pthread_create(&tid, nullptr,sumCount,rq);
    
//     void *ret;
//     pthread_join(tid,&ret);
//     Response *rsp = static_cast<Response *>(ret);
//     cout<<"rsp->result: "<<rsp->result_<<", exitcode: "<<rsp->exitcode_<<endl;
//     delete rsp;
//     return 0;
// }



//part4：

int g_val = 100;

void show(const string &name){
    cout<< name << "say# "
    <<"hello thread"<<endl;
}

void *threadRoutine(void *args){
    const char* name = (const char*)args;
    int cnt = 5;
    while(true){
        printf("%s, pid: %d, g_val: %d, &g_val: 0x%p\n",name, getpid(), g_val,&g_val);
        sleep(1);
        cnt--;
        if(cnt ==0 ) break;   
    }
    int* ret= new int(100);
    return ret;
}

int main(){
    pthread_t tid;
    pthread_create(&tid,nullptr,threadRoutine,(void*)"Thread 1");
    sleep(1);
    // pthread_cancel(tid);
    
    // return 0;
    while(true){
        printf("main thread pid: %d, g_val: %d, &g_val: 0x%p,create new thread tid: %p\n",getpid(),g_val,&g_val,tid);
        // cout<<"main thread, pid: "<<getpid()<<", g_val"<<g_val<<" ,&g_val:"<<&g_val<<endl;
        // show("[main thread]");
        sleep(1);
        g_val++;
        if(g_val==102) break;
    }
    // sleep(7);
    void *retval;
    pthread_join(tid,&retval);
    cout<<"main thread quit ..., ret: "<<*(long long*)retval<<endl;
    return 0;
}

